package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.EmptyUtil;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @Description RoleServiceImpl
 * @Author Lwj
 * @Date 2024-09-03-9:59
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 角色分页
     *
     * @param roleDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<RoleVo> findRoleVoPage(RoleDto roleDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<RoleVo> page = roleMapper.selectPage(roleDto);
        /*PageResponse<RoleVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());*/
        PageResponse<RoleVo> pageResponse = PageResponse.of(page, RoleVo.class);
        return pageResponse;
    }

    /**
     * 角色添加
     *
     * @param roleDto
     */
    @Override
    public void createRole(RoleDto roleDto) {
        //吧releVo转换为role
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.insert(role);
    }

    /**
     * 角色修改
     *
     * @param roleDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateRole(RoleDto roleDto) {
        //吧releVo转换为role
        Role role = BeanUtil.toBean(roleDto, Role.class);
        //1、直接修改角色信息
        roleMapper.updateByPrimaryKeySelective(role);
        //2、判断需不需要修改角色资源
        if (ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())) {
            //如果为空，表示没有资源权限，则直接返回，不可以没有权限
            return true;
        }
        //不为空，首先要删除原有的资源
        roleResourceMapper.deleteByPrimaryKey(roleDto.getId());
        //拿到要修改的资源数组
        String[] checkedResourceNos = roleDto.getCheckedResourceNos();
        //定义一个List来存要修改的资源，方便用来便利修改
        List<RoleResource> resourceNos = new ArrayList<>();
        //把checkedResourceNos转为List来进行便利到resourceNos里面
        List<String> resourceNosList = Arrays.asList(checkedResourceNos);
        resourceNosList.forEach(resourceNo -> {
            RoleResource roleResource = RoleResource.builder().roleId(roleDto.getId()).resourceNo(resourceNo).dataState(SuperConstant.DATA_STATE_0).build();
            //逐条添加到resourceNos
            resourceNos.add(roleResource);
        });

        //如果集合为空，则结束请求
        if (EmptyUtil.isNullOrEmpty(resourceNos)) {
            return true;
        }

        //4.2.1 调用批量新增中间表mapper
        roleResourceMapper.batchInsert(resourceNos);

        return true;

    }

    /**
     * 根据角色查询选中的资源数据
     *
     * @param roleId
     * @return
     */
    @Override
    public Set<String> findCheckedResources(Long roleId) {
        return roleResourceMapper.selectResourceNoByRoleId(roleId);
    }

    /**
     * 角色下拉框
     *
     * @return
     */
    @Override
    public List<RoleVo> RoledropdownBox() {
        List<Role> roleList = roleMapper.SelectRoledropdownBox();
        List<RoleVo> roleVos = new ArrayList<>();
        for (Role role : roleList) {
            RoleVo roleVo = BeanUtil.toBean(role, RoleVo.class);
            roleVos.add(roleVo);
        }
        return roleVos;
    }

    /**
     * 删除角色
     *
     * @param roleId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteRoleById(Long roleId) {
        // 删除角色与菜单关联
        roleResourceMapper.deleteRoleResourceByRoleId(roleId);
        // 再删除角色数据
        roleMapper.deleteByPrimaryKey(roleId);
        //删除角色与用户的中间表数据
        return userRoleMapper.deleteByRoleId(roleId);
    }
}
